<template>
  <el-card class="form-container" shadow="never">
    <el-form
      :model="banner"
      :rules="rules"
      ref="bannerFrom"
      label-width="200px"
      size="small"
    >
      <el-form-item label="广告图片(图片尺寸686*320):">
        <single-upload v-model="banner.picUrl"></single-upload>
      </el-form-item>
      <el-form-item label="跳转路径:">
        <el-input v-model="banner.toUrl" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="排序：">
        <el-input v-model="banner.orderby" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="是否门店页广告:">
        <el-select
          v-model="banner.isZuoti"
          class="input-width"
          placeholder="全部"
          clearable
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('bannerFrom')"
          >提交</el-button
        >
        <el-button v-if="!isEdit" @click="resetForm('bannerFrom')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import SingleUpload from "@/components/Upload/singleUpload";
import { createBanner, updateBanner, getBanner } from "@/api/dms/drivermanage";

const defaultBanner = {
  picUrl: null,
  orderby: null,
};

export default {
  name: "BannerDetail",
  components: { SingleUpload },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      banner: null,
      rules: {
        picUrl: [
          { required: true, message: "请选择广告图片", trigger: "blur" },
        ],
      },
      statusOptions: [
        {
          label: "否",
          value: 0
        },
        {
          label: "是",
          value: 1
        }
      ]
    };
  },
  created() {
    if (this.isEdit) {
      getBanner(this.$route.query.id).then((response) => {
        this.banner = response.data;
      });
    } else {
      this.banner = Object.assign({}, defaultBanner);
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            if (this.isEdit) {
              updateBanner(this.$route.query.id, this.banner).then(
                (response) => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: "修改成功",
                    type: "success",
                    duration: 1000,
                  });
                  this.$router.back();
                }
              );
            } else {
              createBanner(this.banner).then((response) => {
                this.$refs[formName].resetFields();
                this.banner = Object.assign({}, defaultBanner);
                this.$message({
                  message: "提交成功",
                  type: "success",
                  duration: 1000,
                });
              });
            }
          });
        } else {
          this.$message({
            message: "验证失败",
            type: "error",
            duration: 1000,
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.banner = Object.assign({}, defaultBanner);
    },
  },
};
</script>
<style scoped>
.input-width {
  width: 70%;
}
</style>


